package com.ljp.chat.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ljp.chat.entity.pojo.Account;
import com.ljp.chat.exception.WarnException;
import com.ljp.chat.service.AccountService;
import org.springframework.web.bind.annotation.*;
import com.ljp.chat.support.enhancement.RestSupportAdapter;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LvJunPeng
 * @since 2022-11-02
 */
@RestController
@RequestMapping("/account")
public class AccountController extends RestSupportAdapter<Account, AccountService> {

    /**
     * Login map.
     * 登陆接口
     *
     * @param account  the account
     * @param password the password
     * @return the map
     */
    @GetMapping("login")
    public String login(@RequestParam String account, @RequestParam String password) {
        return service.login(account, password);
    }

    /**
     * Logout boolean.
     * 注销接口
     *
     * @return the boolean
     */
    @GetMapping("logout")
    public boolean logout() throws WarnException {
        if (null == StpUtil.getLoginIdDefaultNull()) {
            throw new WarnException("请先登陆");
        }
        StpUtil.logout(StpUtil.getLoginIdDefaultNull());
        return true;
    }

    @PutMapping
    public boolean updateAccount(@RequestBody @Valid Account account) throws Exception {
        return service.updateAccount(account);
    }

}
